package main

import (
	"context"
	"fmt"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"gluenet/modules/manager/stream"
	"gluenet/pkg/log/v1"
	"os"
	"os/signal"
	"path/filepath"
	"syscall"
)

func main() {
	// enable Ctrl C to quit beautifully
	ctx, cancel := context.WithCancel(context.Background())
	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sig
		cancel()
	}()

	server := stream.NewServer(ctx)

	program := filepath.Base(os.Args[0])
	cmd := &cobra.Command{
		Use:   program,
		Short: program + " is manager program execute entry",
		RunE: func(cmd *cobra.Command, args []string) error {
			vpr := viper.New()
			vpr.AutomaticEnv()
			server.ViperConfig(vpr)

			if err := vpr.BindPFlags(cmd.Flags()); err != nil {
				return fmt.Errorf("viper bind pflags %v", err)
			}

			server.InitViper(vpr)

			// initialize and run
			server.Initialize()

			server.Close()
			return nil
		},
	}

	server.InitFlag(cmd.Flags())
	cmd.Flags().String("mode", "test", "running model")

	if err := cmd.Execute(); err != nil {
		v1.Errorf("command execute %v", err)
		cancel()
	}
}
